package com.cwj.we.module.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.cwj.we.R;
import com.cwj.we.base.BaseActivity;
import com.cwj.we.base.BasePresenter;
import com.cwj.we.base.BaseRVAdapter;
import com.cwj.we.base.BaseRVHolder;
import com.cwj.we.bean.Recommend;
import com.cwj.we.utils.FileUtils;
import com.cwj.we.utils.OneClickThree;
import com.cwj.we.utils.ToastUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.gyf.immersionbar.ImmersionBar;

import java.lang.reflect.Type;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.leancloud.LCObject;
import cn.leancloud.LCQuery;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * 影视推荐
 */
public class YingshiRecommendActivity extends BaseActivity {

    @BindView(R.id.ll_back)
    LinearLayout llBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.rvRecommend)
    RecyclerView rvRecommend;
    @BindView(R.id.tv)
    TextView tv;
    @BindView(R.id.llNoData)
    LinearLayout llNoData;
    @BindView(R.id.llHead)
    LinearLayout llHead;

    private BaseRVAdapter<Recommend> adapter;
    private Intent intent;

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_yingshi_recommend;
    }

    @Override
    public void initData() {
        LCQuery<LCObject> query = new LCQuery<>("Recommend");
        query.getInBackground("64780a39aa61440c5e6f795e").subscribe(new Observer<LCObject>() {
            public void onSubscribe(Disposable disposable) {

            }

            public void onNext(LCObject todo) {
                String recommend = todo.getString("recommend");
                if (recommend != null && !"".equals(recommend)) {
                    llNoData.setVisibility(View.GONE);
                    rvRecommend.setVisibility(View.VISIBLE);
                    Type type = new TypeToken<List<Recommend>>() {
                    }.getType();
                    List<Recommend> list = new Gson().fromJson(recommend, type);
                    adapter.setNewData(list);
                } else {
                    llNoData.setVisibility(View.VISIBLE);
                    rvRecommend.setVisibility(View.GONE);
                    ToastUtil.showTextToast(YingshiRecommendActivity.this, "暂无推荐");
                }
            }

            public void onError(Throwable throwable) {
                ToastUtil.showTextToast(YingshiRecommendActivity.this, throwable.getMessage());
            }

            public void onComplete() {
            }
        });

        adapter = new BaseRVAdapter<Recommend>(R.layout.item_recommend) {
            @Override
            public void onBindVH(BaseRVHolder holder, Recommend data, int position) {
                ImageView ivImg = holder.getView(R.id.ivImg);
                LinearLayout llContext = holder.getView(R.id.llContext);
                TextView tvName = holder.getView(R.id.tvName);

                Glide.with(YingshiRecommendActivity.this).load(data.getImg()).placeholder(R.drawable.icon_byj).into(ivImg);
                tvName.setText(data.getName());
                tvName.requestFocus();
                tvName.setSelected(true);

                //影视播放
                llContext.setOnClickListener(v -> {
                    if ("mp4".equals(FileUtils.getExtensionName(data.getUrl()))) {//通过mp4链接播放
                        if (!OneClickThree.isFastClick()) {
                            intent = new Intent(YingshiRecommendActivity.this, VideoPlayActivity.class);
                            intent.putExtra("data", data);
                            startActivity(intent);
                        }
                    } else {//通过网页播放
                        if (!OneClickThree.isFastClick()) {
                            intent = new Intent(YingshiRecommendActivity.this, WebViewActivity.class);
                            intent.putExtra("url", data.getUrl());
                            startActivity(intent);
                        }
                    }
                });
            }
        };
        //设置适配器
        rvRecommend.setAdapter(adapter);
        rvRecommend.setLayoutManager(new GridLayoutManager(this, 3));//列数设置
    }

    @Override
    public void initView() {
        ImmersionBar.with(this)
                .statusBarDarkFont(true)  //状态栏字体是深色，不写默认为亮色
                .init();
        llHead.setBackgroundColor(getResources().getColor(R.color.white));
        tvTitle.setText("影视推荐");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.ll_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_back:
                finish();
                break;
        }
    }
}